#include "Quadrant.h"
#include "Quad.h"
#include "Model.h"
#include "Render.h"
#include "CardDealer.h"

#include "FriendlyShip.h"
#include "Sun.h"
#include "Station.h"
#include "EnemyShip.h"

Quadrant::Quadrant()
{
	action = NOTHING;
	playerShip = Model::getInstance()->getFriendlyShip();
	for(int i=0;i<MAX_UNITS;++i)
		unit[i] = NULL;
	cursor = target = XY(-1);
}

Quadrant::~Quadrant()
{
	clearBoard();
}

void Quadrant::Load(Quad* quad)
{
	currentQuad = quad;
	clearBoard();
	spawnUnits(quad->getQuadInfo());
	fillGrid();
	PrepDraw();
}

void Quadrant::clearBoard()
{
	for(int i=0;i<MAX_UNITS;++i)
		delete unit[i];
}

void Quadrant::spawnUnits(QuadInfo info)
{
	for(int i=0;i<MAX_ENEMIES;++i)
		enemyPos[i] = XY(-1);
	numEnemies = info.Enemies;
	//int count=0;
	//for(int i=0;i<numEnemies;++i, count++)
	//	unit[count] = new EnemyShip;
	//if(info.Stations)
	//{
	//	unit[count] = new Station;
	//	count++;
	//}
	//for(int i=0;i<info.Suns;++i, count++)
	//	unit[count] = new Sun;
}

void Quadrant::fillGrid()
{
	//randomly place units
	CardDealer deck;
	deck.Init(NUM_BOARD_PIECES);

	sector[deck.dealCard()].setUnit(playerShip);

	for(int num = deck.dealCard(), c=0; num != -1; num = deck.dealCard(), c++)
	{
		if(c<MAX_UNITS)
			sector[num].setUnit(unit[c]);
		else
			sector[num].setUnit(NULL);
	}

	//init sectors
	Render* ren = Render::getInstance();
	SpriteInfo info;
	info.numTextures = 2;
	info.reference = ren->LoadTexture("Textures/Blank Tile.png", 45, 45);
	ren->LoadTexture("Textures/Colored Tile.png", 45, 45);

	for(int i=0;i<NUM_BOARD_PIECES;++i)
		sector[i].InitSprite(info);

	int count=0;
	for(int i=0;i<BOARD_SIZE;++i)
		for(int j=0;j<BOARD_SIZE;++j, count++)
			square[i][j] = &sector[count];
}

void Quadrant::ShortRangeScan()
{
	int count = 0;
	for(int i=0;i<BOARD_SIZE;++i)
	{
		for(int j=0;j<BOARD_SIZE;++j)
		{
			XY pos(i,j);
			Unit* unit = ((Sector*)getSquare(pos))->getUnit();
			if(unit)
			{
				switch(unit->getType())
				{
				case ENEMYSHIP:		enemyPos[count] = pos;	((EnemyShip*)unit)->Move(pos);	count++;	break;
				case FRIENDLYSHIP:	playerPos = pos;	playerShip->Move(pos);	break;
				};
			}
		}
	}
}

void Quadrant::Update(MouseInfo info)
{
	XY s = SquareOver(info.pos);
	if(s != cursor)
	{
		Unit* unit = ((Sector*)getSquare(s))->getUnit();
		if(unit)
		{
			switch(unit->getType())
			{
			case SUN:			GreyPath(s);			break;
			case STATION:		ReadyDock(s);			break;
			case ENEMYSHIP:		ReadyMissle(s);			break;
			case FRIENDLYSHIP:	ReadyEnergyWeapon();	break;
			};
		}
		else
			ReadyMovePath(s);
	}
	if(info.Clicked)
	{
		switch(action)
		{
		case MOVE:					MoveShip();			break;
		case NOTHING:									break;
		case FIRE_MISSILE:			FireMissile();		break;
		case FIRE_ENERGY_WEAPON:	FireEnergyWeapon();	break;
		case DOCK:					DockShip();			break;
		};
	}
}

void Quadrant::ReadyEnergyWeapon()
{
	action = FIRE_ENERGY_WEAPON;
}

void Quadrant::ReadyMissle(XY s)
{
	action = FIRE_MISSILE;
}

void Quadrant::ReadyMovePath(XY s)
{
	action = MOVE;
}

void Quadrant::ReadyDock(XY s)
{
	action = DOCK;
}

void Quadrant::GreyPath(XY s)
{
	action = NOTHING;
}

void Quadrant::FireEnergyWeapon()
{
}

void Quadrant::FireMissile()
{
	((Sector*)getSquare(target))->setUnit(NULL);
}

void Quadrant::MoveShip()
{
	playerPos = target;
	playerShip->Move(target);
}

void Quadrant::DockShip()
{
	//calc sector adjacent to target that is closest to ship
	//make that new target
	MoveShip();
	//playerShip->Dock();
}